package com.qf.ssm.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.ssm.qo.CarTypeQo;
import com.qf.ssm.qo.CustomerQO;
import com.qf.ssm.result.ResponseResult;
import com.qf.ssm.result.ResultStatus;
import com.qf.ssm.service.CustomerService;
import com.qf.ssm.vo.CustomerSelectVO;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @author chengqiang
 * @Create 2024-01-02-16:48
 * @Description: 客户的增加
 */
@RestController
@RequestMapping("/customer")
@Api(tags = "客户数据操作接口")
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    @PostMapping
    @ResponseBody
    @ApiOperation("测试接口")
    public String print(){
        System.out.println("da");
        return "连接成功";
    }
    @PostMapping("/save")
    @ResponseBody
    @ApiOperation("添加客户信息接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "customerQO", value = "客户对象")})
    @ApiResponses({@ApiResponse(code = 2000, message = "成功", response = String.class), @ApiResponse(code = 40000, message = "未知错误")})
    public ResponseResult<String> saveCustomer(@RequestBody CustomerQO customerQO){

        return customerService.insertCustomer(customerQO);

    }
    @PostMapping("/img/upload")
    @ResponseBody
    @ApiOperation("文件上传接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "idCard", value = "MultipartFile实体类")})
    @ApiResponses({@ApiResponse(code = 2000, message = "成功", response = String.class), @ApiResponse(code=40009,message="文件上传失败")})
    public ResponseResult<String> upload(@RequestBody MultipartFile idCard,HttpServletRequest req){
        // System.out.println("da"+customerQO);
        String originalFilename = idCard.getOriginalFilename();

        String realPath = req.getServletContext().getRealPath("/WEB-INF/upIdCard");

        File f = new File(realPath);
        if(!f.exists()){
            f.mkdirs();
        }
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileName = UUID.randomUUID().toString()+substring;
        try {
            idCard.transferTo(new File(realPath+fileName));
        } catch (IOException e) {
            return ResponseResult.error(ResultStatus.FILE_UP_ERROR);
        }

        return ResponseResult.success(fileName);

    }
    @PostMapping("/image/download")
    @ResponseBody
    @ApiOperation("文件下载接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "pname", value = "文件名")})
    @ApiResponses({@ApiResponse(code = 2000, message = "成功", response = String.class), @ApiResponse(code = 40000, message = "未知错误")})
    public void download(@RequestParam(value = "pname",required = false)String pname, HttpServletResponse response, HttpServletRequest request){
        try {
            String realPath=request.getServletContext().getRealPath("WEB-INF/upIdCard");
            //获取图片文件
            FileInputStream inputStream = new FileInputStream(new File(realPath+pname));
            //输出流
            ServletOutputStream outputStream = response.getOutputStream();

            response.setContentType("image/jpeg");

            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len = inputStream.read(bytes)) != -1){
                outputStream.write(bytes,0,len);
                outputStream.flush();
            }
            outputStream.close();
            inputStream.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    @PostMapping("/list/individual/{pageNum}/{pageSize}")
    @ApiOperation("查询客户列表接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "customerQO", value = "客户QO对象")})
    @ApiResponses({@ApiResponse(code = 2000, message = "成功", response = CustomerSelectVO.class), @ApiResponse(code = 40000, message = "未知错误")})
    // 根据条件查询全部企业客户列表并分页
    public  ResponseResult<PageInfo<CustomerSelectVO>> listIndividualCustomer( @ApiParam(name = "pageNum",value = "页数",required = true)@PathVariable(value = "pageNum")Integer pageNum,
                                                                               @ApiParam(name = "pageSize",value = "条数",required = true)@PathVariable(value = "pageSize")Integer pageSize,
                                                                               @RequestBody(required = false) CustomerQO customerQO){
        if(pageNum==null){
            pageNum=1;
        }
        if (pageSize==null){
            pageSize=2;
        }
        if(ObjectUtils.isEmpty(customerQO)){
            customerQO=new CustomerQO();
;        }
        customerQO.setCharacters(0);
        PageInfo<CustomerSelectVO> customerSelectVOPageInfo = customerService.queryByCustomer(pageNum, pageSize, customerQO);

        return ResponseResult.success(customerSelectVOPageInfo);

    }

    @PostMapping("/list/enterprise/{pageNum}/{pageSize}")
    @ApiOperation("查询企业客户列表接口")
    @ApiImplicitParams({@ApiImplicitParam(name = "customerQO", value = "客户QO对象")})
    @ApiResponses({@ApiResponse(code = 2000, message = "成功", response = CustomerSelectVO.class), @ApiResponse(code = 40000, message = "未知错误")})
    // 根据条件查询全部企业客户列表并分页
    public  ResponseResult<PageInfo<CustomerSelectVO>> listEnterpriseCustomer( @ApiParam(name = "pageNum",value = "页数",required = true)@PathVariable(value = "pageNum")Integer pageNum,
                                                                               @ApiParam(name = "pageSize",value = "条数",required = true)@PathVariable(value = "pageSize")Integer pageSize,
                                                                               @RequestBody(required = false) CustomerQO customerQO){
        if(pageNum==null){
            pageNum=1;
        }
        if (pageSize==null){
            pageSize=2;
        }
        if(ObjectUtils.isEmpty(customerQO)){
            customerQO=new CustomerQO();
        }

        customerQO.setCharacters(1);
        PageInfo<CustomerSelectVO> customerSelectVOPageInfo = customerService.queryByCustomer(pageNum, pageSize, customerQO);

        return ResponseResult.success(customerSelectVOPageInfo);

    }
    // // 修改用户信息
    @PutMapping("/update")
    @ApiOperation("修改客户信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "customerQO", value = "客户对象")})
    @ApiResponses({@ApiResponse(code = 2000, message = "成功", response = Integer.class),@ApiResponse(code = 2000, message = "修改失败", response = String.class), @ApiResponse(code = 40000, message = "未知错误")})
    public ResponseResult<String>updateCustomer(@RequestBody(required = false) CustomerQO customerQO){
        return customerService.modifyCustomer(customerQO);

    }


}
